package com.sistemagestion.ventas.view;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.sistemagestion.R;
import com.sistemagestion.BO.ProductoBO;
import com.sistemagestion.Entities.DetalleFactura;
import com.sistemagestion.Entities.Producto;

public class FacturaItemAdapter extends ArrayAdapter<DetalleFactura>{

	private int res;
	private List<DetalleFactura> lista;
	
	private ProductoBO productobo = new ProductoBO();	
	
	public FacturaItemAdapter(Context context, int textViewResourceId,
			List<DetalleFactura> objects) {
		super(context, textViewResourceId, objects);
		res = textViewResourceId;
		this.lista = objects;
		
	}
	
	@Override
	public int getCount() {
		return lista.size();
	}
	
	@Override
	public DetalleFactura getItem(int position) {
		return lista.get(position);
	}
	
	@Override
	public void add(DetalleFactura object) {
		lista.add(object);	
		notifyDataSetChanged();
	}
	
	@Override
	public void remove(DetalleFactura object) {
		lista.remove(object);
		notifyDataSetChanged();
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder viewholder;
		if (convertView == null){
			viewholder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(res, null);
			viewholder.txtCodigo = (TextView)convertView.findViewById(R.id.txtCodigo);
			viewholder.txtDescripcion= (TextView)convertView.findViewById(R.id.txtDescripcion);
			viewholder.txtCantidad = (TextView)convertView.findViewById(R.id.txtCantidad);
			viewholder.txtSubtotal = (TextView)convertView.findViewById(R.id.txtSubtotal);
			convertView.setTag(viewholder);
		}
		else{
			viewholder = (ViewHolder)convertView.getTag();
		}
		
		DetalleFactura detalle = (DetalleFactura)getItem(position);
		Producto producto = productobo.getById(detalle.getIdProducto());
		
		viewholder.txtCodigo.setText(Integer.toString(producto.getCodigo()));
		viewholder.txtDescripcion.setText(producto.getNombre());
		viewholder.txtCantidad.setText(Float.toString(detalle.getCantidad()));
		viewholder.txtSubtotal.setText(Float.toString(detalle.getSubTotal()));
		return convertView;
	}
	
	private static class ViewHolder{
		TextView txtCodigo;
		TextView txtDescripcion;
		TextView txtCantidad;
		TextView txtSubtotal;
	}
}
